




;; 函数注释扩展
(define-skeleton document-funtion-header
  "Function header"
  nil

"/**\n"
" *  @brief     "_"\n"
" *\n"
" *  @param[in]  \n"
" *  @param[out] \n"
" *  @return \n"
" */"
  )

;; 文件头注释扩展
(define-skeleton document-file-header
  "file header"
  nil
"/**\n"
" *  @file          " (buffer-name) "\n"
" *  @date          Time-stamp: <>\n"
" *  @brief         " _ "\n"
" *  @version       1.0.0\n"
;;" *  @author        Liangyaozhan\n"
" *  @copyright     guirock team\n"
" *                 ivws02@gmail.com\n"
" *  \n"
" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
" * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
" * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
" * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
" * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
" * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
" * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
" * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
" * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
" * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
" * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
" */"
  )

;; 文件头注释扩展 部分
(define-skeleton document-file-header-part
  "file header part"
  nil
"\n"
  )

;; main函数扩展
(define-skeleton zerlier-skeleton-c-mode-main
  "create int main(int argc, char * argv[]) automatic"
  nil
  "int main (int argc, char * argv[]) \n{\n"
  > _  "\n" > "return 0;"
  "\n}")

;; #include <> c扩展
(define-skeleton zerlier-skeleton-c-mode-include-c-mode
  "create #include <>" nil
  "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n\nint main (int argc, char * argv[]) \n{\n"
  > _  "\n" > "return 0;"
  "\n}")


;; 全局注释扩展
(define-skeleton document-global-comment
  "global comment"
  nil
"/*********************************************************************************************************\n"
" **   " _ "\n"
" ********************************************************************************************************/"
  )

(defun document-get-count-of-spaces-to-72 ()
  "None"
  (end-of-line)
  (setq pRight (point) )
  (beginning-of-line)
  (setq pLeft (point) )
  (end-of-line)
  (- pRight pLeft)
)

(defun document-get-white-chars (n) 
  "print n spaces."
  (setq i 0)
  (setq spaces "")
  (while 
      (< i n)
    (setq i (+ i 1))
    (setq spaces (concat spaces " "))
    ) 
  spaces
)

;; 注释扩展
(define-skeleton document-tail-comment
  "tail comment"
  nil
  >
  (document-get-white-chars (- 40 (document-get-count-of-spaces-to-72) ))
  "/*!  " _ "*/"
 )

;; 局部注释扩展
(define-skeleton document-local-comment
  "local comment"
  nil
  "/*\n"  "*  " > _"\n"  "*/" > )


;; for 扩展
(define-skeleton zerlier-skeleton-c-mode-for
  "create for () {}" nil
  "for ( " _ ";; )\n " > "{">"\n" > "}" >)

;; 询问且填充的 for 扩展
(define-skeleton zerlier-skeleton-c-mode-for-fill
  "create for () {}" nil
  "for (" > (setq v1 (read-from-minibuffer "Iterator: " )) " = 0; " v1 " < " 
  (read-from-minibuffer "condition: " ) "; " v1 "++)\n " > "{\n" > _ "\n" > "}" > )

;; 询问扩展 struct ( c++ )
(define-skeleton zerlier-skeleton-c-mode-struct-c++
  "create struct" nil
  "struct " > (setq v1(read-from-minibuffer "struct name: ")) "\n{" > "\n" > _ "\n};")

;; 询问扩展 struct 
(define-skeleton zerlier-skeleton-c-mode-struct-c
  "create struct" nil
  "struct __" > (setq v1(read-from-minibuffer "struct name: ")) "\n{" > "\n" > _ "\n};\n" > "typedef struct __" v1 " " v1 ";\n" )

;; 询问扩展 classs
(define-skeleton zerlier-skeleton-c-mode-class-c++
  "create class" nil
  "class " > (setq v1(read-from-minibuffer "class name: "))
  "\n{" > "\n"
  "public:" > "\n"
  v1 "();" > "\n"
  "virtual ~" v1 "();" > "\n"
  "\n"
  "public:" > "\n"
  "protected:" > "\n"
  "private:" > "\n"
  > _ "\n};\n" )

;; 这个是例子，米用
(define-skeleton ywb-skeleton-perl-open
  "这个例子显示如何在 skeleton 里使用 v1 变量，以及 lisp 表达式。
  会根据输入的文件变量是否是一个完整的变量而加上引号，如果前面是 >，
  会写上 create 而不是 open。例如：
  open(FH, $file) or die \"Can't open file $file: $!\";
  open(FH, \">$file\") or die \"Can't create file $file: $!\";
  "
  nil
  "open(" (skeleton-read "File handle: " "FH") ", "
  (progn (setq v1 (read-from-minibuffer "File: " ))
         (if (string-match "^\\$[A-Za-z_0-9]+$" v1)
             v1
           (concat "\"" v1 "\"")))
  ") or die \"Can't "
  (if (= (aref v1 0) ?>)
      (concat "create file " (substring v1 1))
    (concat "open file " v1))
  ": $!\";\n")

;; ifelse 扩展
(define-skeleton zerlier-skeleton-c-mode-ifelse
  "create if () {}" nil
  "if ( " _ " )\n{" > "\n" > "}" > "\n else " > "\n {" > "\n"> "}" > "\n"  > )
  
;; switch 扩展
(define-skeleton zerlier-skeleton-c-mode-switch
  "create switch () {}" nil
  > "switch ( " _ " )\n{">"\n" > "case :" > "\n" > "\n" > "break;" 
  > "\n\n" > "default:" > "\n" > "\n" > "break;" > "\n" > "}"  > "\n" > )


;; if 扩展
(define-skeleton zerlier-skeleton-c-mode-if
  "create if () {}" nil
  "if ( " _ " )\n{">"\n" > "}" > "\n" > )


;; while 扩展
(define-skeleton zerlier-skeleton-c-mode-while
  "create while () {}" nil
  "while ( " _ " )\n{">"\n" > "}" >)

;; printf 扩展
(define-skeleton zerlier-skeleton-c-mode-printf
  "create printf(\"\");" nil
  "printf( \"" _ "\" );" )

;; scanf 扩展
(define-skeleton zerlier-skeleton-c-mode-scanf
  "create scanf(\"\", );" nil
  "scanf( \"" _ "\", );" )

;; dowhile 扩展
(define-skeleton zerlier-skeleton-c-mode-dowhile
  "create do{}while ();" nil
  "do {" > "\n" > "\n" "}" > " while ( " _ " );\n" )

;; headerfile 扩展
(define-skeleton zerlier-skeleton-c-mode-headerfile
  "create headerfile" nil
  
  "/**\n"
  " *  @file          " (buffer-name) "\n"
  " *  @date          Time-stamp: <>\n"
  " *  @brief         " _ "\n"
  " *  @version       1.0.0\n"
  " *  @author        Liangyaozhan\n"
  " *  @copyright     guirock team\n"
  " *                 ivws02@gmail.com\n"
  " */"
  "\n\n"
  "\n#ifndef " (setq v1 (replace-regexp-in-string "\\." "_"
                                                       (format "__%s__"(upcase (buffer-name)))) )
  "\n#define " v1 "\n\n#include \"\"\n\n"
  "#ifdef __cplusplus\n"
  "extern \"C\" {\n"
  "#endif                                                                  /* __cplusplus                  */\n"
  "\n\n\n"
  "#ifdef __cplusplus\n"
  "}\n"
  "#endif                                                                  /*  __cplusplus                 */\n"
  "\n#endif"
  "                                                                  "
  "/*  " v1 (document-get-white-chars (- 27 (length v1)) )  " */\n" 
  )


(define-abbrev-table 'c++-mode-abbrev-table '(
("mainx" "" zerlier-skeleton-c-mode-main 0)
("inclu" "" zerlier-skeleton-c-mode-include-c-mode 0)
("structx" "" zerlier-skeleton-c-mode-struct-c 0)
("functionheader" "" document-funtion-header 0)
("fileheader" "" document-file-header 0)
("functionh" "" document-funtion-header 0)
("fileh" "" document-file-header 0)
("filehp" "" document-file-header-part 0)
("gcomment" "" document-global-comment 0)
("lcomment" "" document-local-comment 0)
("tcomment" "" document-tail-comment 0)
("gcmt" "" document-global-comment 0)
("lcmt" "" document-local-comment 0)
("tcmt" "" document-tail-comment 0)
("forr" "" zerlier-skeleton-c-mode-for 0)
("fori" "" zerlier-skeleton-c-mode-for-fill 0)
("iff" "" zerlier-skeleton-c-mode-if 0)
("ifelse" "" zerlier-skeleton-c-mode-ifelse 0)
("switchh" "" zerlier-skeleton-c-mode-switch 0)
("whilee" "" zerlier-skeleton-c-mode-while 0)
("dowhile" "" zerlier-skeleton-c-mode-dowhile 0)
("headerfile" "" zerlier-skeleton-c-mode-headerfile 0)
("printff" "" zerlier-skeleton-c-mode-printf 0)
("scanff" "" zerlier-skeleton-c-mode-scanf 0)
))

(define-abbrev-table 'c-mode-abbrev-table '(
("mainx" "" zerlier-skeleton-c-mode-main 0)
("inclu" "" zerlier-skeleton-c-mode-include-c-mode 0)
("structx" "" zerlier-skeleton-c-mode-struct-c 0)
("functionheader" "" document-funtion-header 0)
("fileheader" "" document-file-header 0)
("functionh" "" document-funtion-header 0)
("fileh" "" document-file-header 0)
("filehp" "" document-file-header-part 0)
("gcomment" "" document-global-comment 0)
("lcomment" "" document-local-comment 0)
("tcomment" "" document-tail-comment 0)
("gcmt" "" document-global-comment 0)
("lcmt" "" document-local-comment 0)
("tcmt" "" document-tail-comment 0)
("forr" "" zerlier-skeleton-c-mode-for 0)
("fori" "" zerlier-skeleton-c-mode-for-fill 0)
("iff" "" zerlier-skeleton-c-mode-if 0)
("ifelse" "" zerlier-skeleton-c-mode-ifelse 0)
("switchh" "" zerlier-skeleton-c-mode-switch 0)
("whilee" "" zerlier-skeleton-c-mode-while 0)
("dowhile" "" zerlier-skeleton-c-mode-dowhile 0)
("headerfile" "" zerlier-skeleton-c-mode-headerfile 0)
("hfile" "" zerlier-skeleton-c-mode-headerfile 0)
("printff" "" zerlier-skeleton-c-mode-printf 0)
("scanff" "" zerlier-skeleton-c-mode-scanf 0)
("classs" "" zerlier-skeleton-c-mode-class-c++ 0)
))
